const { contextBridge, ipcRenderer } = require("electron");

// 暴露安全的 API 给渲染进程
contextBridge.exposeInMainWorld("electronAPI", {
  // 音频循环回放相关
  enableLoopbackAudio: () => ipcRenderer.invoke("enable-loopback-audio"),
  disableLoopbackAudio: () => ipcRenderer.invoke("disable-loopback-audio"),

  // 应用信息
  getAppInfo: () => ({
    platform: process.platform,
    arch: process.arch,
    version: process.versions.electron,
  }),

  // 日志记录
  log: (level, message) => ipcRenderer.invoke("log", level, message),

  // 错误处理
  onError: (callback) => ipcRenderer.on("error", callback),
  removeErrorListener: () => ipcRenderer.removeAllListeners("error"),
});

// 处理未捕获的错误
window.addEventListener("error", (event) => {
  console.error("渲染进程错误:", event.error);
});

window.addEventListener("unhandledrejection", (event) => {
  console.error("未处理的 Promise 拒绝:", event.reason);
});
